import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockDemo {
    private static int number = 0;
    static ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public static void main(String[] args) {

        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    get();
                }
            }).start();
            if (i == 80) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        set(80);
                    }
                }).start();
            }
        }

    }


    public static int get() {
        readWriteLock.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + "--" + number);
            return number;
        } finally {
            readWriteLock.readLock().unlock();
        }
    }

    public static void set(int i) {
        readWriteLock.writeLock().lock();
        try {
            number = i;
            System.out.println(Thread.currentThread().getName() + "--" + number);
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
}
